package com.water.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author wsj
 * @version 1.0
 * @description: TODO
 * @date 2022/1/11 21:01
 */
public class MyCglibProxy {
    public static void main(String[] args) {
        final NBAService nbaService = new NBAService();
        //不需要业务方法实现接口，直接增强
        NBAService service = (NBAService)Enhancer.create(nbaService.getClass(),
                new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        System.out.println("======开启事务===========");
                        Object invoke = methodProxy.invokeSuper(o, objects);
                        System.out.println("======提交事务========");
                        return invoke;
                    }
                });

        service.add();
    }
}
